package com.wu.mapper;

import com.wu.App;
import com.wu.entity.Blog;
import com.wu.util.IDUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class blog {
    @Test
    public void addInitBlog() {
        SqlSession session = App.getSession();
        BlogMapper mapper = session.getMapper(BlogMapper.class);
     Blog blog = new Blog();
     blog.setId(IDUtil.genId());
     blog.setTitle("Mybatis如此简单");
     blog.setAuthor("吴承恩");
     blog.setCreateTime(new Date());
     blog.setViews(9999);
        int i = mapper.addBlog(blog);
        session.commit();
        session.close();
    }
    @Test
    public void testQueryBlogIf(){
    SqlSession session = App.getSession();
    BlogMapper mapper = session.getMapper(BlogMapper.class);
        HashMap<String, String> map = new HashMap<String, String>();
//        map.put("title","Mybatis如此简单");
        map.put("author","吴承恩");
        List<Blog> blogs = mapper.queryBlogIf(map);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
    }
    @Test
    public void testupdateBlog(){
        SqlSession session = App.getSession();
        BlogMapper mapper = session.getMapper(BlogMapper.class);
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id","1");
        map.put("author","吴帅哥");
        int i = mapper.updateBlog(map);
        session.commit();
        session.close();
    }
    @Test
    public void queryBlogChoose(){
        SqlSession session = App.getSession();
        BlogMapper mapper = session.getMapper(BlogMapper.class);
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("title","my");
        map.put("author","吴承恩");
        map.put("views",9);
        List<Blog> blogs = mapper.queryBlogChoose(map);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
    }

    @Test
    public void queryBlogForeach(){
        SqlSession session = App.getSession();
        BlogMapper mapper = session.getMapper(BlogMapper.class);
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(1);
        ids.add(2);
        ids.add(3);
        map.put("ids" ,ids);
        List<Blog> blogs = mapper.queryBlogForeach(map);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
    }

}